package xnj.lazydog.btcontroller.ControlViews.ControlViews;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import xnj.lazydog.btcontroller.ControlViews.ControlView;
import xnj.lazydog.btcontroller.ControlViews.Value;
import xnj.lazydog.btcontroller.ControlViews.ViewOptions;
import xnj.lazydog.btcontroller.R;

public class ControlSwitch extends ControlElement {

    private static final String TAG = "ControlSwitch";
    public ControlSwitch (ViewOptions options)
    {
        super(options);
        //Log.w(TAG, "ControlSwitch");
        p.setStrokeWidth(ControlView.mm2Pix(0.66f));
        p.setTextAlign(Paint.Align.CENTER);
        //p.setTextSize();
    }


    @Override
    public void showSettingsDialog(Context context) {


        if(linkedValues[0]==null) {
            Toast.makeText(context,"请先链接一个变量",Toast.LENGTH_SHORT).show();
            return;
        }
        final EditText on_edit,off_edit;
        Button ok;
        TextView info;
        String str;
        final AlertDialog dialog;
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        View layout = LayoutInflater.from(context).inflate(R.layout.toy_settings_switch,null);
        builder.setView(layout);
        dialog = builder.create();

        if(dialog.getWindow()!=null)
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        on_edit = layout.findViewById(R.id.on_edit);
        off_edit = layout.findViewById(R.id.off_edit);
        info = layout.findViewById(R.id.button_info);
        ok = layout.findViewById(R.id.button);

        if(options.on_en)
        {
            if(linkedValues[0].type<4)
                str=(""+(int)(options.on_value));
            else
                str=(""+options.on_value);
        }else
            str="";

        on_edit.setText(str);
        on_edit.setSelection(str.length());

        if(options.off_en)
        {
            if(linkedValues[0].type<4)
                str=(""+(int)(options.off_value));
            else
                str=(""+options.off_value);
        }else
            str ="";

        off_edit.setText(str);
        off_edit.setSelection(str.length());

        if(linkedValues[0].type<4)
            str = "此开关链接的"+valuesName[0]+"为"+type_name[linkedValues[0].type]+"型,数值不得超过"+(int)(MIN_VALUES[linkedValues[0].type])+"~"+(int)(MAX_VALUES[linkedValues[0].type]);
        else
            str = "此开关链接的"+valuesName[0]+"为"+type_name[linkedValues[0].type]+"型,数值不得超过"+(MIN_VALUES[linkedValues[0].type])+"~"+(MAX_VALUES[linkedValues[0].type]);

        info.setText(str);

        initLimitedEdit(on_edit,info,linkedValues[0].type);
        initLimitedEdit(off_edit,info,linkedValues[0].type);

        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Value value = getNumber(on_edit.getText().toString());
                if(value.type==0)
                {
                    options.on_value = 0;
                    options.on_en = true;
                }else
                {
                    options.on_value = value.getFloat();
                    options.on_en = true;
                }

                value = getNumber(off_edit.getText().toString());
                if(value.type==0)
                {
                    options.off_value = 0;
                    options.off_en = true;
                }else
                {
                    options.off_value = value.getFloat();
                    options.off_en = true;
                }

                options.threshold = (options.on_value+options.off_value)*0.5f;
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    float check_x = 0;

    RectF track_rect = new RectF();

    final float TRACK_W_RATIO_HALF = 0.32f;
    final float TRACK_H_RATIO_HALF = 0.22f;
    final float TRACK_Y_RATIO = -0.15f;
    final float THUMB_RATIO_HALF = 0.32f;




    float real_w_pix,real_h_pix;
    float thumb_radius_pix;
    float track_w_half_pix,track_h_half_pix;
    float track_y_off_pix;


    float last_text_size;
    @Override
    public void onSizeChanged() {
        real_w_pix = this.w_pix;
        real_h_pix = this.h_pix;
        if(real_w_pix>real_h_pix*1.5f)
            real_w_pix = real_h_pix*1.5f;
        else
            real_h_pix = real_w_pix*0.667f;


        track_rect.right = real_w_pix*TRACK_W_RATIO_HALF;
        track_rect.left  = -track_rect.right;

        track_rect.bottom = real_h_pix*(TRACK_H_RATIO_HALF+TRACK_Y_RATIO);
        track_rect.top = real_h_pix*(-TRACK_H_RATIO_HALF+TRACK_Y_RATIO);

        thumb_radius_pix = real_h_pix*THUMB_RATIO_HALF;

        track_h_half_pix = real_h_pix*TRACK_H_RATIO_HALF;
        track_y_off_pix = real_h_pix*TRACK_Y_RATIO;

        text_height_pix = real_h_pix*TEXT_SIZE_RATIO;
        if(text_height_pix<ControlView.mm2Pix(MIN_TEXT_MM))
            text_height_pix = ControlView.mm2Pix(MIN_TEXT_MM);
        if(text_height_pix>ControlView.mm2Pix(MAX_TEXT_MM))
            text_height_pix = ControlView.mm2Pix(MAX_TEXT_MM);

        if(Math.abs(text_height_pix-last_text_size)>5)
        {
            p.setTextSize(text_height_pix);

            last_text_size = text_height_pix;
        }
        text_y_pix = real_h_pix*0.4f+text_height_pix*0.5f;
    }

    boolean isBusy = true ;
    float delta;
    float check_x_set;

    int thumb_color;
    int edge_color;
    boolean last_value;
    boolean this_value;
    @Override
    public void draw(Canvas canvas) {


        if(linkedValues[0]==null)
        {
            this_value = isPressed;
        }else
        {
            this_value = linkedValues[0].getBoolean(options.threshold);
        }

        if(isBusy) {
                if (this_value)
                    check_x_set = 0.25f;
                else
                    check_x_set = -0.25f;

            delta = check_x_set - check_x;

            if (Math.abs(delta) > 0.02f) {
                check_x += delta * anim_k;
            } else {
                isBusy = false;
                check_x = check_x_set;
            }
        }else
        {
            if(linkedValues[0]!=null) {
                if (last_value!=linkedValues[0].getBoolean(options.threshold))
                    isBusy = true;
            }
        }
        last_value = this_value;



        p.setStyle(Paint.Style.FILL);
        p.setColor(0xffbbbbbb);
        canvas.drawRoundRect(track_rect,track_h_half_pix,track_h_half_pix,p);

        if(isBusy) {
            thumb_color = getMiddleColor(ViewOptions.THEME_EDGE_COLOR, 0xffffffff, (check_x + 0.25f) * 2);
            edge_color = getMiddleColor(0xffffffff,ViewOptions.THEME_EDGE_COLOR,(check_x+0.25f)*2);
        }

        p.setColor(thumb_color);
        canvas.drawCircle(real_w_pix*check_x,track_y_off_pix,thumb_radius_pix,p);



//            if (linkedValues[0] == null)
//                canvas.drawText("BUTTON", 0, text_y0, p);
//            else{
//                if (valuesName[0] != null)
//                    canvas.drawText(valuesName[0], 0, text_y1, p);
//                canvas.drawText("" + linkedValues[0].get(), 0, text_y2, p);
//            }


        p.setColor(0xff444444);
        if (linkedValues[0] == null)
            canvas.drawText("开关",0,text_y_pix,p);
        else
        {
            if (valuesName[0]!= null)
            {
                canvas.drawText(valuesName[0]+":"+linkedValues[0].get(),0,text_y_pix,p);
            }
        }

        p.setStyle(Paint.Style.STROKE);


        p.setColor(edge_color);
        canvas.drawCircle(real_w_pix*check_x,track_y_off_pix,thumb_radius_pix,p);


    }

    @Override
    public void clicked(Context context) {

        //Log.w(TAG, "clicked");

        if(linkedValues[0]!=null)
        {

            if(linkedValues[0].getBoolean(options.threshold))
                linkedValues[0].set(options.off_value);
            else
                linkedValues[0].set(options.on_value);

            isBusy = true;
            triggerDataEvent();
        }

    }

    @Override
    public void dragged() {

    }

    @Override
    public void released() {

    }

    @Override
    public void pressed() {


    }

    @Override
    public void zoomed(float sx, float sy) {

    }
}
